const app = getApp().globalData
const baseUrl = app.domain
import { setCache } from './util'
let requestStack = []
import { codeLogin } from '../api/index'
import { tansParams } from '../api/comm'
export default function request(param, check = false, toast = true,urlGet=false) { 
    param.url = param.url.includes('http') ? param.url : (baseUrl + param.url)
    if(urlGet){
      param.url =param.url + '?' + tansParams(param.data);
    }
    param.header = {
      'content-type': 'application/json;charset=UTF-8',
      //'content-type': 'application/x-www-form-urlencoded',
      //'X-TOKEN': app.x_tk,
      'token': app.x_tk,
      'APPDEV': app.isDev ? 1 : 0
    }
    return new Promise((resolve, reject) => {
      if (check) {
        wx.showLoading({
          title: '',
          mask: false
        })
      }
      wx.request({
        ...param,
        success: res => {
        },
        fail: res => {
          reject(res.data)
        },
        complete: (res) => {
          if (res.statusCode == 200) {
            wx.stopPullDownRefresh()
            wx.hideLoading()
            switch (res.data.status) {
              case 0:
                if (toast) {
                  wx.showToast({
                    icon: 'none',
                    title: res.data.message,
                    success: () => {
                      reject(res.data)
                    }
                  })
                } else {
                  reject(res.data)
                }
                break
              case 1:
                resolve(res.data)
                break
              case 2:
                resolve(res.data)
                break
              case 10:
                console.log('获取个人信息');
                wx.getUserProfile({
                  desc: '获取个人信息',
                  success:(res)=>{
                    console.log(res);
                  }

                })
                
                break
              case 100:
                // wx.showToast({
                //   icon: 'none',
                //   title: res.data.message,
                // })
                requestStack.push({ param, check, toast,urlGet, resolve: resolve})
                app.x_tk = ''
                if (!app.hasgologin) {
                  app.hasgologin = true
                  setTimeout(() => {
                    app.hasgologin = false
                  }, 1000);
                  app.isLogin = false
                  app.isReload = true
                  app.userInfo = ''
                  setCache('userInfo', '')
                  setCache('isLogin', false)
                  wx.login({
                    success: (res) => {
                      codeLogin({code: res.code}).then((res2) => {
                        if (res2.datas.is_user == 0) {
                          wx.navigateTo({
                            url: '/pages/index/login/index'
                          })
                          return
                        }
                        app.hasgologin = false
                        setCache('x_tk', res2.datas.api_token)
                        setCache('userInfo', res2.datas)
                        setCache('isLogin', true)
                        app.x_tk = res2.datas.api_token
                        app.isLogin = true
                        app.userInfo = res2.datas
                        requestStack.forEach((it) => {
                          request(it.param, it.check, it.toast,it.urlGet).then((rr) => {
                            it.resolve(rr)
                          })
                        })
                        setTimeout(() => {
                          requestStack = []
                        }, 1000);
                      })
                    }
                  })
                  // setTimeout(() => {
                  //   wx.navigateTo({
                  //     url: '/pages/index/login/index'
                  //   })
                  // }, 1000);
                }
                break
              default:
                wx.showToast({
                  icon: 'none',
                  title: res.data.message || res.data.message,
                  success: () => {
                    reject(res.data)
                  }
                })
                break
            }
          } else {
            wx.stopPullDownRefresh()
            wx.hideLoading()
          }
        }
      })
    })
  }